package com.hd.trans.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;

import com.hd.trans.ui.base.AppContextProvider;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by chan on 2016/11/9.
 * <p>
 * 文件处理工具类
 */

public class FileUtil {

    private static final String FILE_ORIG_PATH = "SDK/File/Orig/";

    private static final String FILE_TARGET_PATH = "SDK/File/Target/";

    private static final String FILE_DOWNLOAD_PATH = "SDK/File/Download/";

    public static String getCacheRoot(){
        String path;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            path = AppContextProvider.getContext().getFilesDir().getAbsolutePath() + "/";
        }else {
            path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
        }
        return path;
    }

    public static String getFileOrigPath() {
        String path = getCacheRoot() + FILE_ORIG_PATH;
        File output = new File(path);
        if (!output.exists()) {
            output.mkdirs();
        }
        return path;
    }

    public static String getFileTargetPath() {
        String path = getCacheRoot() + FILE_TARGET_PATH;
        File output = new File(path);
        if (!output.exists()) {
            output.mkdirs();
        }
        return path;
    }

    public static String getFileDownloadPath() {
        String path = getCacheRoot() + FILE_DOWNLOAD_PATH;
        File output = new File(path);
        if (!output.exists()) {
            output.mkdirs();
        }
        return path;
    }

    public static void writeFile(String content, String path, boolean isAppend) {
        File file = new File(path);
        FileWriter fw = null;
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            try {
                fw = new FileWriter(file);
                fw.write(content);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //删除文件
    public static void deleteFile(String path) {
        File file = new File(path);
        if(file.exists() && file.isFile()){
            file.delete();
        }
    }

    //删除文件
    public static boolean deleteFile2(File file) {
        if (file.exists()) {
            return file.delete();
        }
        return false;
    }

    public static String getAppPath(Context context) {
        return context.getExternalFilesDir(null).getAbsolutePath();
    }

    public static String getPath(Context context, Uri uri) {
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {"_data"};
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it  Or Log it.
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

}
